package com.isoftstone.app.controller.bNotify;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.notify.BNotify;
import com.isoftstone.merchant.service.notice.impl.NotifyService;
import com.isoftstone.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知接口控制类
 * Created by gxy on 2017/12/14.
 */
@Controller
public class BNotifyController extends BaseController {

    @Autowired
    NotifyService notifyService;

    /**
     * 根据用户登录名,查询用户 我的消息未读数量
     *
     * @param openAccount 登录用户名
     * @return 通知集合
     */
    @RequestMapping(value = "app/admin/getNReadNotifyNum", method = RequestMethod.POST)
    @ResponseBody
    public Tidings<Object> getNReadNotifyNum(String openAccount) {
        Tidings<Object> Tidings = new Tidings<Object>();
        try {
            List<BNotify> bNotifyList = notifyService.getNReadNotifyNum(Const.readn, Const.send, openAccount);
            Tidings.setStatus(Const.SUCCESS);
            Tidings.setT(bNotifyList);
        } catch (Exception e) {
            Tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return Tidings;
    }

    /**
     * 根据用户登录名,查询用户 我的消息内明细
     *
     * @param openAccount 登录用户名
     * @return 通知集合
     */
    @RequestMapping(value = "app/admin/getNReadNotifyList", method = RequestMethod.POST)
    @ResponseBody
    public Tidings<Map<String, Object>> getNReadNotifyList(String openAccount) {
        Tidings<Map<String, Object>> listTiding = new Tidings<Map<String, Object>>();
        Map<String, Object> reMap = new HashMap<String, Object>();
        try {
            List<BNotify> bNotifyList1 = notifyService.getNReadNotifyList(Const.readn, Const.n0001, Const.send, openAccount);
            if (bNotifyList1 == null || bNotifyList1.size() == 0) {
                bNotifyList1 = notifyService.getReadNotifyList(Const.ready, Const.n0001, Const.send, openAccount);
            }
            reMap.put("bNotifyList1", bNotifyList1);
            List<BNotify> bNotifyList2 = notifyService.getNReadNotifyList(Const.readn, Const.n0003, Const.send, openAccount);
            if (bNotifyList2 == null || bNotifyList2.size() == 0) {
                bNotifyList2 = notifyService.getReadNotifyList(Const.ready, Const.n0003, Const.send, openAccount);
            }
            reMap.put("bNotifyList2", bNotifyList2);
            List<BNotify> bNotifyList3 = notifyService.getNReadNotifyList(Const.readn, Const.n0002, Const.send, openAccount);
            if (bNotifyList3 == null || bNotifyList3.size() == 0) {
                bNotifyList3 = notifyService.getReadNotifyList(Const.ready, Const.n0002, Const.send, openAccount);
            }
            reMap.put("bNotifyList3", bNotifyList3);
            listTiding.setStatus(Const.SUCCESS);
            listTiding.setT(reMap);
        } catch (Exception e) {
            listTiding.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return listTiding;
    }

    /**
     * 根据用户登录名和通知类型,查询通知明细
     *
     * @param openAccount 登录用户名
     * @param type        类型
     * @return 通知集合
     */
    @RequestMapping(value = "app/admin/getNotifyList", method = RequestMethod.POST)
    @ResponseBody
    public Tidings<Map<String, Object>> getNotifyList(String type, String openAccount) {
        Tidings<Map<String, Object>> listTiding = new Tidings<Map<String, Object>>();
        Map<String, Object> reMap = new HashMap<String, Object>();
        List<BNotify> bNotifyListN = null;
        List<BNotify> bNotifyListY = null;
        try {
            if (type.equals("0001")) {
                bNotifyListN = notifyService.getNotifyListOne(Const.readn, type, Const.send, openAccount);
                bNotifyListY = notifyService.getNotifyListOne(Const.ready, type, Const.send, openAccount);
            }
            if (type.equals("0002")) {
                bNotifyListN = notifyService.getNotifyListOne(Const.readn, type, Const.send, openAccount);
                bNotifyListY = notifyService.getNotifyListOne(Const.ready, type, Const.send, openAccount);
            }
            if (type.equals("0003")) {
                bNotifyListN = notifyService.getNotifyListTwo(Const.readn, type, Const.send, openAccount);
                bNotifyListY = notifyService.getNotifyListTwo(Const.ready, type, Const.send, openAccount);
            }
            reMap.put("bNotifyListN", bNotifyListN);
            reMap.put("bNotifyListY", bNotifyListY);
            listTiding.setStatus(Const.SUCCESS);
            listTiding.setT(reMap);
        } catch (Exception e) {
            listTiding.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return listTiding;
    }

    /**
     * 根据用户登录名,查询用户 普通通知
     * @param openAccount 登录用户名
     * @param read 读取状态
     * @return 通知集合
     */
//    @RequestMapping(value = "app/bNotify/getNotifyListOne" ,method = RequestMethod.POST)
//    @ResponseBody
//    public Tidings<List<BNotify>>  getNotifyListOne(String read, String openAccount){
//        Tidings<List<BNotify>> listTiding = new  Tidings<List<BNotify>> ();
//        try {
//            List<BNotify> bNotifyList = notifyService.getNotifyListOne(read, Const.n0001,openAccount);
//            listTiding.setStatus(Const.SUCCESS);
//            listTiding.setT(bNotifyList);
//        } catch (Exception e) {
//            listTiding.setStatus(Const.ERROR);
//            e.printStackTrace();
//        }
//        return listTiding ;
//    }

    /**
     * 根据用户登录名,查询用户 签约通知
     * @param openAccount 登录用户名
     * @param read 读取状态
     * @return 通知集合
     */
//    @RequestMapping(value = "app/bNotify/getNotifyListTwo" ,method = RequestMethod.POST)
//    @ResponseBody
//    public Tidings<List<BNotify>>  getNotifyListTwo(String read, String openAccount){
//        Tidings<List<BNotify>> listTiding = new  Tidings<List<BNotify>> ();
//        try {
//            List<BNotify> bNotifyList = notifyService.getNotifyListOne(read,Const.n0002,openAccount);
//            listTiding.setStatus(Const.SUCCESS);
//            listTiding.setT(bNotifyList);
//        } catch (Exception e) {
//            listTiding.setStatus(Const.ERROR);
//            e.printStackTrace();
//        }
//        return listTiding ;
//    }

    /**
     * 根据用户登录名,查询用户 缴费通知
     * @param openAccount 登录用户名
     * @param read 读取状态
     * @return 通知集合
     */
//    @RequestMapping(value = "app/bNotify/getNotifyListThree" ,method = RequestMethod.POST)
//    @ResponseBody
//    public Tidings<List<BNotify>>  getNotifyListThree(String read, String openAccount){
//        Tidings<List<BNotify>> listTiding = new  Tidings<List<BNotify>> ();
//        try {
//            List<BNotify> bNotifyList = notifyService.getNotifyListTwo(read,Const.n0003,openAccount);
//            listTiding.setStatus(Const.SUCCESS);
//            listTiding.setT(bNotifyList);
//        } catch (Exception e) {
//            listTiding.setStatus(Const.ERROR);
//            e.printStackTrace();
//        }
//        return listTiding ;
//    }

    /**
     * 根据用户Id和消息Id,修改通知读取状态，未读修改成已读
     *
     * @param notifyId 消息Id
     * @param userId   用户Id
     * @return 通知集合
     */
    @RequestMapping(value = "app/admin/updateNotifyRead", method = RequestMethod.POST)
    @ResponseBody
    public Tidings<Object> updateNotifyRead(String phoneType, String notifyId, String userId, String openAccount) {
        Tidings<Object> Tidings = new Tidings<Object>();
        try {
            if (phoneType.equals(Const.n0001)) {
                notifyService.updateNotifyReadById(Const.ready, notifyId, openAccount);
            }
            if(phoneType.equals(Const.n0002)||phoneType.equals(Const.n0003)){
                notifyService.updateNotifyRead(Const.ready, notifyId, userId);
            }
            Tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            Tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return Tidings;
    }
}
